Logging the calling functionΒΆ

import logging

def log(func):

    ''' Logging the calling function '''
    def wrap_log(*args, **kwargs):

        name = func.__name__
        logger = logging.getLogger(name)
        logger.setLevel(logging.INFO)

        # Open log file for writing
        fh = logging.FileHandler("%s.log" % name)
        fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
        formatter = logging.Formatter(fmt)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

        logger.info("Call function: %s" % name)
        result = func(*args, **kwargs)
        logger.info("Result: %s" % result)
        return func

    return wrap_log

@log
def double_function(a):
    ''' Multiply the given parameter '''
    return a * 2

if __name__ == "__main__":
    value = double_function(2)

Result:

The new double_function.log file in the current directory
File Contents:
  2018-10-05 01:08:31,041 - double_function - INFO - Call function: double_function
  2018-10-05 01:08:31,041 - double_function - INFO - Result: 4